package com.timepies;

import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.timepies.button.Dropdown;
import com.timepies.button.PieAbortButton;
import com.timepies.button.PieConfirmButton;
import com.timepies.button.PieDatePicker;
import com.timepies.button.PieListView;
import com.timepies.button.PieStaticListView;
import com.timepies.persistence.DbManager;
import com.timepies.util.Easter.YearOutOfRangeException;
import com.timepies.util.Festivita;

public class ElencoPieActivity extends Activity implements OnNavigationListener {
	private static final String TAG = ElencoPieActivity.class.getSimpleName();
	private DbManager db = null;
	private Dialog editDialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pie_elenco);

		db = new DbManager(this);
		Dropdown.addTo(this);
		Log.d(TAG, "onCreate complete!");

	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		TextView mainlistTitle = (TextView) this
				.findViewById(R.id.mainlist_title);
		try {
			String[] array = Festivita.countFesteAndPonti().split(",");
			if (itemPosition == 1) {
				String title = "Nel " + array[0] + " su un totale di "
						+ array[1] + " giorni di festa, " + array[2]
						+ " cadono in giorni lavorativi.";
				PieStaticListView.addTo(this, Festivita.calcolaProssimeFeste());
				mainlistTitle.setText(title);
			}
			if (itemPosition == 2) {
				int tot = Integer.parseInt(array[1])
						+ Integer.parseInt(array[3]);
				String title = "Nel "
						+ array[0]
						+ " su un totale di "
						+ array[1]
						+ " giorni di festa, "
						+ array[2]
						+ " cadono in giorni lavorativi. I giorni di festa salgono a "
						+ tot + " se si sommano i " + array[3]
						+ " giorni di ponti.";
				PieStaticListView.addTo(this, Festivita.calcolaProssimiPonti());
				mainlistTitle.setText(title);
			}
			if (itemPosition == 0) {
				PieListView.addTo(this);
				mainlistTitle.setText(this.getText(R.string.mainlist_title));
			}

			return true;

		} catch (YearOutOfRangeException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.mainlist_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Log.d(TAG, "MenuItem: " + item.getItemId());
		switch (item.getItemId()) {

		case R.id.action_settings:
			startActivity(new Intent(ElencoPieActivity.this,
					SettingsActivity.class));
			finish();
			break;
		case R.id.action_about:
			startActivity(new Intent(ElencoPieActivity.this,
					SplashActivity.class));
			finish();
			break;

		case R.id.action_refresh:
			Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
			PieListView.addTo(this);
			break;

		case R.id.action_add:
			Log.d(TAG, "buildDialog start");
			this.editDialog = new Dialog(this);
			this.editDialog
					.setContentView(R.layout.activity_pie_dettaglio_dialog);
			this.editDialog.setTitle("Il tuo evento speciale");
			this.editDialog.show();
			PieDatePicker.addTo(this);
			PieAbortButton.addTo(this);
			PieConfirmButton.addTo(this);
			break;
		}

		return true;

	}

	@Override
	public void onBackPressed() {
		Builder finishDialog = new Builder(this);
		finishDialog.setTitle("Abbandona Pie");
		finishDialog.setMessage("Sicuro di voler uscire?");
		finishDialog.setPositiveButton("Si",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int button) {
						ElencoPieActivity.this.finish();
					}
				});
		finishDialog.setNegativeButton("No", null).show();
	}

	public DbManager getDb() {
		return db;
	}

	public Dialog getEditDialog() {
		return editDialog;
	}

}